package com.zgm.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zgm.utils.ui.MyDialog;

import java.lang.reflect.Method;

/**
 * Created by 张公铭 on 2017/5/23 0023.
 */

public class UiUtils
{
    public static void ToastInCenter(Context context, String message, int showTime)
    {
        Toast toast = Toast.makeText(context, message, showTime);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    /**
     * 权限设置提醒
     */
    public static void showPermission(final Context context, String message, DialogInterface.OnClickListener listener)
    {
        new MyDialog.Builder(context).setMessage(message, 0).setNegativeButton("取消", listener, 0).setPosiviteButton("设置", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                context.startActivity(intent);
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        }, 0).create().show();
    }

    /**
     * 单选按钮的Dialog
     */
    public static void showDialog(Context context, String title, String message)
    {
        showDialog(context, title, message, "确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        }, null, null);
    }

    /**
     * 带2个按钮
     *
     * @param context
     * @param title
     * @param message
     * @param positiveText
     * @param posiveOnclickListener
     * @param negativeText
     * @param negativeOnclickListener
     */
    public static void showDialog(Context context, String title, String message, String positiveText, DialogInterface.OnClickListener posiveOnclickListener,
                                  String negativeText, DialogInterface.OnClickListener negativeOnclickListener)
    {
        new MyDialog.Builder(context).setTitle(title, 0).setMessage(message, 0).setPosiviteButton(positiveText, posiveOnclickListener, 0)
                .setNegativeButton(negativeText, negativeOnclickListener, 0).create().show();
    }

    public static void showDialog(Context context, String title, String message, String positiveText, DialogInterface.OnClickListener posiveOnclickListener)
    {
        showDialog(context, title, message, positiveText, posiveOnclickListener, null, null);
    }

    /**
     * 禁止软键盘弹出，并显示光标
     */
    public static void disableShowSoftInput(EditText view)
    {
        if (android.os.Build.VERSION.SDK_INT <= 10)
        {
            view.setInputType(InputType.TYPE_NULL);
        } else
        {
            Class<EditText> cls = EditText.class;
            Method method;
            try
            {
                method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(view, false);
            } catch (Exception e)
            {
            }

            try
            {
                method = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(view, false);
            } catch (Exception e)
            {
            }
        }
    }

    /**
     * 文本框获得焦点，。
     */
    public static void editTextFocus(EditText ed)
    {
        editTextFocus(ed, ed.getText().length());
    }

    /**
     * 文本框获得焦点，。
     */
    public static void editTextFocus(EditText ed, int index)
    {
        ed.setEnabled(true);
        ed.setFocusable(true);
        ed.setFocusableInTouchMode(true);
        ed.requestFocus();
        ed.setSelection(index);
    }

    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
